home *** CD-ROM | disk | FTP | other *** search
/ Kit PC World De Ampliacion De Windows 95 / Kit PC World de ampliacion de Windows 95.iso / internet / sweeper / samples / progress / progress.cpp < prev    next >
C/C++ Source or Header  |  1995-12-05  |  12KB  |  339 lines

  1. // ===========================================================================
  2. // File: PROGRESS.CPP
  3. // 
  4. // Copyright 1995 - 1996 Microsoft Corporation.  All Rights Reserved.
  5. // ===========================================================================
  6. #include <urlmon.h>
  7. #include "resource.h"
  8.  
  9. // %%Classes: ----------------------------------------------------------------
  10. class CDownload {
  11.   public:
  12.     CDownload(LPCWSTR szURL);
  13.     HRESULT DoDownload(HWND,HWND,HWND);
  14.     LPCWSTR              m_url;
  15.  
  16.   private:
  17.     IMoniker*            m_pmk;
  18.     IBindCtx*            m_pbc;
  19.     IBindStatusCallback* m_pbsc;
  20. };
  21.  
  22. class CBindStatusCallback : public IBindStatusCallback {
  23.   public:
  24.     // IUnknown methods
  25.     STDMETHODIMP    QueryInterface(REFIID riid,void ** ppv);
  26.     STDMETHODIMP_(ULONG)    AddRef()    { return m_cRef++; }
  27.     STDMETHODIMP_(ULONG)    Release()   { if (--m_cRef == 0) { delete this; return 0; } return m_cRef; }
  28.  
  29.     // IBindStatusCallback methods
  30.     STDMETHODIMP    GetBindInfo(DWORD* pgrfBINDF, BINDINFO* pbindinfo);
  31.     STDMETHODIMP    OnStartBinding(IBinding* pbinding);
  32.     STDMETHODIMP    GetPriority(LONG* pnPriority);
  33.     STDMETHODIMP    OnProgress(ULONG ulProgress, ULONG ulProgressMax, ULONG ulStatusCode,
  34.                         LPCWSTR pwzStatusText);
  35.     STDMETHODIMP    OnDataAvailable(DWORD grfBSCF, DWORD dwSize, FORMATETC *pFmtetc,
  36.                         REFIID riid, IUnknown* punk);
  37.     STDMETHODIMP    OnLowResource(DWORD dwReserved);
  38.     STDMETHODIMP    OnStopBinding(HRESULT hrResult);
  39.  
  40.     // constructors/destructors
  41.     CBindStatusCallback(HWND hwndStatus, HWND hwndProgress, HWND hwndText);
  42.  
  43.     inline void SetStatus(LPCWSTR szStatus)     { SetWndText(m_hwndStatus, szStatus); }
  44.     void SetProgress(LPCWSTR szProgress)        { SetWndText(m_hwndProgress, szProgress); }
  45.     void SetWndText(HWND hwnd, LPCWSTR szText);
  46.  
  47.     // data members
  48.     DWORD           m_cRef;
  49.     IBinding*       m_pbinding;
  50.     IStream*        m_pstm;
  51.     HWND            m_hwndStatus;
  52.     HWND            m_hwndProgress;
  53.     HWND            m_hwndText;
  54.     DWORD           m_cbOld;
  55. };
  56.  
  57.  
  58. // ---------------------------------------------------------------------------
  59. // %%Function: CBindStatusCallback::CBindStatusCallback
  60. // ---------------------------------------------------------------------------
  61. CBindStatusCallback::CBindStatusCallback(HWND hwndStatus, HWND hwndProgress, HWND hwndText)
  62. {
  63.     m_hwndStatus = hwndStatus;
  64.     m_hwndProgress = hwndProgress;
  65.     m_hwndText = hwndText;
  66.     m_pbinding = NULL;
  67.     m_pstm = NULL;
  68.     m_cRef = 1;
  69.     m_cbOld = 0;
  70. }  // CBindStatusCallback
  71.  
  72. // ---------------------------------------------------------------------------
  73. // %%Function: CBindStatusCallback::~CBindStatusCallback
  74. // ---------------------------------------------------------------------------
  75. CBindStatusCallback::~CBindStatusCallback()
  76. {
  77.     if (m_pstm)
  78.         m_pstm->Release();
  79.     if (m_pbinding)
  80.         m_pbinding->Release();
  81. }  // ~CBindStatusCallback
  82.  
  83. // ---------------------------------------------------------------------------
  84. // %%Function: CBindStatusCallback::SetWndText
  85. // ---------------------------------------------------------------------------
  86.  inline void
  87. CBindStatusCallback::SetWndText(HWND hwnd,LPCWSTR szText)
  88. {
  89.     if (IsWindow(hwnd))
  90.         {
  91.         char    rgchBuf[MAX_PATH];
  92.         WideCharToMultiByte(CP_ACP, 0, szText, -1, rgchBuf, MAX_PATH, 0, 0);
  93.     SetWindowText(hwnd, rgchBuf);
  94.     }
  95. }  // CBindStatusCallback::SetWndText
  96.  
  97. // ---------------------------------------------------------------------------
  98. // %%Function: CBindStatusCallback::QueryInterface
  99. // ---------------------------------------------------------------------------
  100.  STDMETHODIMP
  101. CBindStatusCallback::QueryInterface(REFIID riid, void** ppv)
  102.     *ppv = NULL;
  103.     
  104.     if (riid==IID_IUnknown || riid==IID_IBindStatusCallback)
  105.         {
  106.         *ppv = this;
  107.         AddRef();
  108.         return S_OK;
  109.         }
  110.     return E_NOINTERFACE;
  111. }  // CBindStatusCallback::QueryInterface
  112.  
  113. // ---------------------------------------------------------------------------
  114. // %%Function: CBindStatusCallback::GetBindInfo
  115. // ---------------------------------------------------------------------------
  116.  STDMETHODIMP
  117. CBindStatusCallback::GetBindInfo(DWORD* pgrfBINDF, BINDINFO* pbindInfo)
  118. {
  119.     *pgrfBINDF = BINDF_ASYNCHRONOUS;
  120.     pbindInfo->cbSize = sizeof(BINDINFO);
  121.     pbindInfo->szExtraInfo = NULL;
  122.     memset(&pbindInfo->stgmedPost, 0, sizeof(STGMEDIUM));
  123.     return S_OK;
  124. }  // CBindStatusCallback::GetBindInfo
  125.  
  126. // ---------------------------------------------------------------------------
  127. // %%Function: CBindStatusCallback::OnStartBinding
  128. // ---------------------------------------------------------------------------
  129.  STDMETHODIMP
  130. CBindStatusCallback::OnStartBinding(IBinding* pbinding)
  131. {
  132.     if (m_pbinding != NULL)
  133.         m_pbinding->Release();
  134.     m_pbinding = pbinding;
  135.     if (m_pbinding != NULL)
  136.         {
  137.         m_pbinding->AddRef();
  138.         SetStatus(L"Status: Starting to bind...");
  139.         }
  140.     return S_OK;
  141. }  // CBindStatusCallback::OnStartBinding
  142.  
  143. // ---------------------------------------------------------------------------
  144. // %%Function: CBindStatusCallback::GetPriority
  145. // ---------------------------------------------------------------------------
  146.  STDMETHODIMP
  147. CBindStatusCallback::GetPriority(LONG* pnPriority) 
  148. {
  149.     return E_NOTIMPL;
  150. }  // CBindStatusCallback::GetPriority
  151.  
  152. // ---------------------------------------------------------------------------
  153. // %%Function: CBindStatusCallback::OnProgress
  154. // ---------------------------------------------------------------------------
  155.  STDMETHODIMP
  156. CBindStatusCallback::OnProgress(ULONG ulProgress, ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
  157. {
  158.     char    sz[255];
  159.     WideCharToMultiByte(CP_ACP, 0, szStatusText,-1, sz, 255, 0,0);
  160.  
  161.     char    msg[100];
  162.     WCHAR   out[200];
  163.  
  164.     wsprintf(msg,"Progress: %s %d of %d ", sz, ulProgress, ulProgressMax);
  165.     MultiByteToWideChar(CP_ACP, 0, msg, -1, out, sizeof(out));
  166.  
  167.     SetProgress(out);
  168.     return(NOERROR);
  169. }  // CBindStatusCallback
  170.  
  171. // ---------------------------------------------------------------------------
  172. // %%Function: CBindStatusCallback::OnDataAvailable
  173. // ---------------------------------------------------------------------------
  174.  STDMETHODIMP
  175. CBindStatusCallback::OnDataAvailable(DWORD grfBSCF, DWORD dwSize, FORMATETC *pFmtetc, REFIID riid, IUnknown *punk) 
  176. {
  177.     // Assert(riid == IID_IStream);
  178.     if (!m_pstm)
  179.         m_pstm = (IStream *)punk;
  180.  
  181.     if (dwSize > m_cbOld)
  182.     {
  183.         DWORD dwRead = dwSize - m_cbOld;
  184.         DWORD dwActuallyRead;
  185.         char * lp = new char[dwRead];
  186.         m_pstm->Read(lp,dwRead,&dwActuallyRead);
  187.         SetWindowText(m_hwndText,lp);
  188.         delete lp;
  189.         m_cbOld = dwSize;
  190.     }
  191.     return S_OK;
  192. }  // CBindStatusCallback::OnDataAvailable
  193.  
  194. // ---------------------------------------------------------------------------
  195. // %%Function: CBindStatusCallback::OnLowResource
  196. // ---------------------------------------------------------------------------
  197.  STDMETHODIMP
  198. CBindStatusCallback::OnLowResource(DWORD dwReserved)
  199. {
  200.     return E_NOTIMPL;
  201. }  // CBindStatusCallback::OnLoadResource
  202.  
  203. // ---------------------------------------------------------------------------
  204. // %%Function: CBindStatusCallback::OnStopBinding
  205. // ---------------------------------------------------------------------------
  206.  STDMETHODIMP
  207. CBindStatusCallback::OnStopBinding(HRESULT hrStatus) 
  208. {
  209.     SetStatus(L"Status: Done.");
  210.     return S_OK;
  211. }  // CBindStatusCallback::OnStopBinding
  212.  
  213. // ---------------------------------------------------------------------------
  214. // %%Function: CDownload::CDownload
  215. // ---------------------------------------------------------------------------
  216. CDownload::CDownload(LPCWSTR szURL)
  217. {
  218.     m_url = szURL;
  219.     m_pmk = 0;
  220.     m_pbc = 0;
  221.     m_pbsc = 0;
  222. }  // CDownload
  223.  
  224. // ---------------------------------------------------------------------------
  225. // %%Function: CDownload::~CDownload
  226. // ---------------------------------------------------------------------------
  227. CDownload::~CDownload()
  228. {
  229.     if (m_pmk)
  230.         m_pmk->Release();
  231.     if (m_pbc)
  232.         m_pbc->Release();
  233.     if (m_pbsc)
  234.         m_pbsc->Release();
  235. }  // ~CDownload
  236.  
  237. // ---------------------------------------------------------------------------
  238. // %%Function: CDownload::DoDownload
  239. // ---------------------------------------------------------------------------
  240.  HRESULT
  241. CDownload::DoDownload(HWND hwndStatus, HWND hwndProgress, HWND hwndText)
  242. {
  243.     HRESULT hr =  CreateURLMoniker(NULL, m_url, &m_pmk);
  244.  
  245.     if( SUCCEEDED(hr) )
  246.         {
  247.         m_pbsc = new CBindStatusCallback(hwndStatus, hwndProgress, hwndText);
  248.         if (m_pbsc != NULL)
  249.             m_pbsc->AddRef();
  250.         else
  251.             hr = E_OUTOFMEMORY;
  252.     }
  253.  
  254.     if (SUCCEEDED(hr))
  255.         hr = CreateBindCtx(0, &m_pbc);
  256.  
  257.     if (SUCCEEDED(hr))
  258.         hr = m_pbc->RegisterObjectParam(OLESTR("BindStatusCallback"), m_pbsc);
  259.         
  260.     IStream* pstm;
  261.     if (SUCCEEDED(hr))
  262.         hr = m_pmk->BindToStorage(m_pbc, 0, IID_IStream, (void**)&pstm);
  263.  
  264.     return hr;
  265. }  // CDownload::DoDownload
  266.  
  267. // ---------------------------------------------------------------------------
  268. // %%Function: DialogProc
  269. // ---------------------------------------------------------------------------
  270.  BOOL CALLBACK
  271. DialogProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam)
  272. {
  273.     static CDownload* pcdl = NULL;
  274.  
  275.     switch(message)
  276.         {
  277.         case WM_INITDIALOG:
  278.             EnableWindow(GetDlgItem(hwndDlg, IDOK), TRUE);
  279.             EnableWindow(GetDlgItem(hwndDlg, IDCANCEL), TRUE);
  280.             pcdl = reinterpret_cast<CDownload*>(lParam);
  281.             break;
  282.  
  283.         case WM_COMMAND:
  284.             {
  285.             switch (LOWORD(wParam))
  286.                 {
  287.                 case IDOK:
  288.                     {
  289.                     char rgchBuf[MAX_PATH];
  290.                     HWND hwndStatus = GetDlgItem(hwndDlg, IDC_DISPLAY);
  291.                     HWND hwndProgress = GetDlgItem(hwndDlg, IDC_PROGRESS);
  292.                     HWND hwndText = GetDlgItem(hwndDlg, IDC_TEXT);
  293.  
  294.                     EnableWindow(GetDlgItem(hwndDlg, IDOK), FALSE);
  295.                     EnableWindow(GetDlgItem(hwndDlg, IDCANCEL), FALSE);
  296.  
  297.                     WideCharToMultiByte(CP_ACP, 0, pcdl->m_url, -1, rgchBuf, MAX_PATH, 0, 0);
  298.                 SetWindowText(hwndDlg, rgchBuf);
  299.  
  300.                     SetWindowText(hwndStatus, TEXT("Status: Inititating Bind..."));
  301.                     pcdl->DoDownload(hwndStatus, hwndProgress, hwndText);
  302.                     EnableWindow(GetDlgItem(hwndDlg, IDCANCEL), TRUE);
  303.                     break;
  304.                     }
  305.  
  306.                 case IDCANCEL:
  307.                     EndDialog(hwndDlg,0);
  308.                     return 1;
  309.                 }
  310.             break;
  311.             }
  312.         }
  313.  
  314.     return 0;
  315. }  // DialogProc
  316.  
  317. // ---------------------------------------------------------------------------
  318. // %%Function: WinMain
  319. // ---------------------------------------------------------------------------
  320.  int WINAPI
  321. WinMain(HINSTANCE hinst, HINSTANCE hinstPrev, LPSTR szCmdLine, int nCmdShow)
  322. {
  323.     static WCHAR    szDefaultURL[] = L"http://www.home.msn.com";
  324.     WCHAR           rgwchCmdLine[MAX_PATH];
  325.  
  326.     MultiByteToWideChar(CP_ACP, 0, szCmdLine, -1, rgwchCmdLine, MAX_PATH);
  327.     
  328.     CDownload download(rgwchCmdLine[0] ? rgwchCmdLine : szDefaultURL);
  329.  
  330.     DialogBoxParam(hinst, MAKEINTRESOURCE(IDD_DIALOG1), HWND_DESKTOP,
  331.         (FARPROC)&DialogProc, reinterpret_cast<LPARAM>(&download));
  332.  
  333.     return 0;
  334. }  // WinMain
  335.  
  336. // EOF =======================================================================
  337.  
  338.